package com.hex.es.service.impl;

import com.hex.es.annotation.EsBuildParamTypeInfo;
import com.hex.es.aop.AbstractEsAop;
import com.hex.es.service.ParamBuildInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

/**
 * @description: 排序参数构建器
 * @author: Hex
 * @date: 2019/11/20 16:29
 */
@Slf4j
@Service
public class ParamBuildOrderByImpl extends AbstractEsAop implements ParamBuildInterface {

    @Override
    public void paramBuild(Object key, Object value, BoolQueryBuilder boolQueryBuilder, NativeSearchQueryBuilder searchQueryBuilder, EsBuildParamTypeInfo esBuildParamTypeInfo) {
        if (StringUtils.isNotEmpty(esBuildParamTypeInfo.orderByField())) {
            FieldSortBuilder sortBuilder = SortBuilders.fieldSort(humpToUnderline(esBuildParamTypeInfo.orderByField())).order(esBuildParamTypeInfo.orderDirection());
            searchQueryBuilder.withSort(sortBuilder);
        }
    }
}
